package binarysearch.test74;

/**
 * 搜索二维矩阵
 *
 * @author code_yc
 * @version 1.0
 * @date 2020/7/11 10:29
 */
public class Solution1 {

    // 二分查找
    public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return false;
        int row = matrix.length, col = matrix[0].length;
        int l = 0, r = (row * col) - 1;
        while (l <= r) {
            int m = l + ((r - l) >> 1);
            int a = m / col, b = m % col;
            if (matrix[a][b] < target) {
                l = m + 1;
            } else if (matrix[a][b] > target) {
                r = m - 1;
            } else {
                return true;
            }
        }
        return false;
    }
}
